Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic.ControlChars
Imports DynaViewer.Classes

Public Class Form1

    Friend NotInheritable Class NativeMethods

        Public Sub New()
        End Sub

        <DllImport("user32.dll")> _
        Public Shared Function SendMessage(ByVal hwnd As IntPtr, ByVal wMsg As UInteger, ByVal wParam As UInteger, ByVal lParam As IntPtr) As IntPtr
        End Function
    End Class

    Const WM_VSCROLL As Integer = &H115
    Const SB_BOTTOM As Integer = 7

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        TextBox1.Text = OpenFileDialog1.FileName
        ListView1.Items.Clear()
        Refresh()
        Cursor = Cursors.WaitCursor
        LoadDynaLogFile(OpenFileDialog1.FileName)
        Cursor = Cursors.Arrow
        Button2.Enabled = True
    End Sub

    Sub LoadDynaLogFile(ByVal DynaLogFile As String)
        Label2.Visible = False
        Refresh()
        Dim dlEvent As DynaLogEvent
        If File.Exists(DynaLogFile) Then
            Dim DynaLogLines As String() = File.ReadAllLines(DynaLogFile)
            For Each LogLine As String In DynaLogLines
                dlEvent = LogHelper.ParseEventLine(LogLine)
                If dlEvent IsNot Nothing Then
                    ListView1.Items.Add(New ListViewItem(New String() {dlEvent.EventTimestamp, dlEvent.EventPid, dlEvent.EventCaller, dlEvent.EventMessage}))
                End If
            Next
        Else
            MsgBox("The file " & Quote & DynaLogFile & Quote & " does not exist.", vbOKOnly + vbCritical, Text)
            Exit Sub
        End If
        Label2.Text = String.Format("Number of processed entries: {0}. Double-click an entry to get its information.", ListView1.Items.Count)
        Label2.Visible = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If String.IsNullOrEmpty(TextBox1.Text) OrElse Not File.Exists(TextBox1.Text) Then
            Button2.Enabled = False
            Exit Sub
        End If
        ListView1.Items.Clear()
        Refresh()
        Cursor = Cursors.WaitCursor
        LoadDynaLogFile(TextBox1.Text)
        Cursor = Cursors.Arrow
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Environment.GetCommandLineArgs().Length > 0 Then
            For Each CommandArgument As String In Environment.GetCommandLineArgs()
                If CommandArgument.Equals(Environment.GetCommandLineArgs()(0), StringComparison.OrdinalIgnoreCase) Then
                    Continue For
                End If

                If Not CommandArgument.StartsWith("/", StringComparison.OrdinalIgnoreCase) AndAlso File.Exists(CommandArgument) Then
                    TextBox1.Text = CommandArgument.Trim()
                    ListView1.Items.Clear()
                    Refresh()
                    Cursor = Cursors.WaitCursor
                    LoadDynaLogFile(CommandArgument.Trim())
                    Cursor = Cursors.Arrow
                    Button2.Enabled = True
                ElseIf Not CommandArgument.StartsWith("/", StringComparison.OrdinalIgnoreCase) AndAlso Not File.Exists(CommandArgument) Then
                    MsgBox("The file " & Quote & CommandArgument & Quote & " does not exist.", vbOKOnly + vbCritical, Text)
                    Exit Sub
                Else
                    If CommandArgument.StartsWith("/selectfirst=", StringComparison.OrdinalIgnoreCase) Then
                        If ListView1.Items.Count > 0 Then
                            Try
                                Dim SelectedItemCount As Integer = CInt(CommandArgument.Replace("/selectfirst=", "").Trim())
                                For i As Integer = 0 To SelectedItemCount - 1
                                    ListView1.Items(i).Selected = True
                                Next
                            Catch ex As Exception

                            End Try
                            ListView1.Select()
                        End If
                    ElseIf CommandArgument.StartsWith("/selectlast=", StringComparison.OrdinalIgnoreCase) Then
                        If ListView1.Items.Count > 0 Then
                            Try
                                Dim SelectedItemCount As Integer = CInt(CommandArgument.Replace("/selectlast=", "").Trim())
                                For i As Integer = 0 To SelectedItemCount - 1
                                    ListView1.Items(ListView1.Items.Count - 1 - i).Selected = True
                                Next
                            Catch ex As Exception

                            End Try
                            ListView1.Select()
                            NativeMethods.SendMessage(ListView1.Handle, WM_VSCROLL, SB_BOTTOM, IntPtr.Zero)
                        End If
                    End If
                End If
            Next
        End If
    End Sub

    Private Sub ListView1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
        If e.Button = Windows.Forms.MouseButtons.Left AndAlso ListView1.SelectedItems.Count = 1 Then
            EventProperties.Label1.Text = String.Format("Information for event {0} of {1}:", ListView1.FocusedItem.Index + 1, ListView1.Items.Count)
            EventProperties.txtEventTimestamp.Text = ListView1.FocusedItem.SubItems(0).Text
            EventProperties.Label6.Text = String.Format("PID {0}", ListView1.FocusedItem.SubItems(1).Text)
            Dim evtCallerParts As String() = ListView1.FocusedItem.SubItems(2).Text.Replace(" (", " ").Trim().Split(" ")
            EventProperties.txtEventCaller.Text = evtCallerParts(0)
            If evtCallerParts.Length = 2 Then
                EventProperties.txtEventParentCaller.Text = evtCallerParts(1).TrimEnd(")")
            Else
                EventProperties.txtEventParentCaller.Text = ""
            End If
            EventProperties.txtEventMessage.Text = ListView1.FocusedItem.SubItems(3).Text

            EventProperties.CurrentEventIndex = ListView1.FocusedItem.Index
            EventProperties.EventCount = ListView1.Items.Count
            EventProperties.ShowDialog(Me)
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
#If VBC_VER >= 9.0 Then
        MsgBox(String.Format("DynaLog Log Viewer (DynaViewer) version {0}" & CrLf & CrLf & "{1}", _
                My.Application.Info.Version.ToString() & "_" & RetrieveLinkerTimestamp().ToString("yyMMdd-HHmm") , _
                My.Application.Info.Copyright), _
            vbOKOnly + vbInformation, Text)
#Else
        MsgBox(String.Format("DynaLog Log Viewer (DynaViewer) version {0}_NET2REL" & CrLf & CrLf & "{1}", _
                My.Application.Info.Version.ToString(), _
                My.Application.Info.Copyright), _
            vbOKOnly + vbInformation, Text)
#End If
    End Sub
End Class
